<template>
  <el-dialog :title="form_component.formTitle" :visible.sync="form_component.dialogFormVisible" @close="close_form">
    <el-alert title="如需修改学号请删除后重新添加" type="warning" v-show="form_component.formTitle.includes('修改')"></el-alert>
    <br>
    <el-form :model="form_component.formData" :label-width="form_component.formLabelWidth">
      <el-row>
        <el-col :span="12">
          <el-form-item label="学院" >
            <el-input v-model="form_component.formData.department" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专业">
            <el-input v-model="form_component.formData.profession" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="年级">
            <el-input v-model="form_component.formData.grade" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="班级">
            <el-input v-model="form_component.formData.className" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="姓名">
            <el-input v-model="form_component.formData.stuName" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="学号">
            <el-input v-model="form_component.formData.stuNumber" autocomplete="off" :disabled="form_component.formTitle.includes('修改')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="身份证">
            <el-input v-model="form_component.formData.stuIdNumber" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别">
            <el-input v-model="form_component.formData.gender" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="入学年级">
            <el-input v-model="form_component.formData.admissionGrade" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="归属年级">
            <el-input v-model="form_component.formData.belongGrade" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="学制">
            <el-input v-model="form_component.formData.educationalSystem" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="培养层次">
            <el-input v-model="form_component.formData.cultureLevel" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="教育类型">
            <el-input v-model="form_component.formData.educationalType" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态">
            <el-input v-model="form_component.formData.status" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input v-model="form_component.formData.systemDescription" autocomplete="off"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close_form">取 消</el-button>
      <el-button type="primary" @click="submit_info">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: 'Form',
    props: ['form_component'],
    data() {
      return {

      }
    },
    methods: {
      close_form(){
        this.form_component.dialogFormVisible = false;
        this.$emit('reload_data_request')
      },
      submit_info(){
        if (this.form_component.formData.stuNumber===undefined||this.form_component.formData.stuNumber.length===0){
          this.$message({
            type: 'error',
            message: '必须输入学号'
          });
          return;
        }
        if (this.form_component.formTitle.includes('新增')){
          this.$ajax.post(this.$server + "/student/insert",this.form_component.formData)
            .then(res => {
              if(res.data){
                this.$message({
                  type: 'success',
                  message: '添加成功'
                });
                this.form_component.dialogFormVisible = false;//关闭窗口
                //请求重新加载页面
                this.$emit('reload_data_request')
              }else {
                this.$message({
                  type: 'error',
                  message: '学号已存在，添加失败'
                });
              }
            })
            .catch(error => {
              console.log("发生错误：" + error)
              this.msg = error
            });
        }else if (this.form_component.formTitle.includes('修改')){
          this.$ajax.post(this.$server + "/student/modify",this.form_component.formData).then(res => {
            if(res.data){
              this.$message({
                type: 'success',
                message: '修改成功'
              });
              this.form_component.dialogFormVisible = false;//关闭窗口
              //请求重新加载页面
              this.$emit('reload_data_request')
            }else {
              this.$message({
                type: 'error',
                message: '修改失败'
              });
            }
          }).catch(error => {
            console.log("发生错误：" + error)
            this.msg = error
          });
        }
      }
    },
    //在父组件注册子组件
    components: {

    },
    created() {//组件创建完成时获取查询次数

    }
  }
</script>
